package com.ruoyi.common.push;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.CommonEnum;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.AudienceDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.Strategy;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.getui.push.v2.sdk.dto.res.TaskIdDTO;

/**
 * 	批量推送
 * @author DEREK
 *
 */
public class UniBatchPushUtil {
	private static PushApi pushApi;



	/**
	 * 	创建消息
	 * @param title			标题
	 * @param content		内容
	 * @param groupName		分组名
	 */
	public static String createMsg(String title,String content,String groupName) {
		String taskId = "";
		GtApiConfiguration apiConfiguration = new GtApiConfiguration();  
		//填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取  
		apiConfiguration.setAppId("i300GFeLTF6icXtakKFEZ2");  
		apiConfiguration.setAppKey("End3B8Z2wJ8mNHZ066OLF2");  
		apiConfiguration.setMasterSecret("cVT43JPilc6X7JCs1tVlz2");  
		apiConfiguration.setDomain("https://restapi.getui.com/v2/");  
		// 实例化ApiHelper对象，用于创建接口对象  
		ApiHelper apiHelper = ApiHelper.build(apiConfiguration);  
		// 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi  
		pushApi = apiHelper.creatApi(PushApi.class);  
		PushDTO<Audience> pushDTO = new PushDTO<Audience>();
		pushDTO.setRequestId(System.currentTimeMillis() + "");
		pushDTO.setGroupName(groupName);
		Settings settings = new Settings();
		settings.setTtl(3600000);
		Strategy strategy = new Strategy();
		strategy.setSt(1);
		settings.setStrategy(strategy);
		pushDTO.setSettings(settings);
		PushMessage pushMessage = new PushMessage();
		GTNotification notification = new GTNotification();
//		notification.setLogoUrl("https://url");
		notification.setTitle(title);
		notification.setBody(content);
		notification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
//		notification.setUrl("https//:www.getui.com");
		pushMessage.setNotification(notification);
		pushDTO.setPushMessage(pushMessage);
		PushChannel pushChannel = new PushChannel();
		AndroidDTO androidDTO = new AndroidDTO();
		Ups ups = new Ups();
		Map<String, Map<String, Object>> options = new HashMap<String, Map<String,Object>>();
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("/message/android/notification/badge/class", "io.dcloud.PandoraEntry");
		map.put("/message/android/notification/badge/add_num", 1);
		options.put("HW", map);
		ups.setOptions(options);
		ThirdNotification thirdNotification = new ThirdNotification();
		thirdNotification.setClickType(CommonEnum.ClickTypeEnum.TYPE_STARTAPP.type);
		thirdNotification.setTitle(title);
		thirdNotification.setBody(content);
		ups.setNotification(thirdNotification);
		androidDTO.setUps(ups);
		pushChannel.setAndroid(androidDTO);
		IosDTO iosDTO = new IosDTO();
		Aps aps = new Aps();
		Alert alert = new Alert();
		alert.setTitle(title);
		alert.setBody(content);
		aps.setAlert(alert);
		iosDTO.setAps(aps);
		pushChannel.setIos(iosDTO);
		pushDTO.setPushChannel(pushChannel);
		ApiResult<TaskIdDTO> createMsg = pushApi.createMsg(pushDTO);
		System.out.println(createMsg);
		if(createMsg.getCode() == 0) {
			taskId =  createMsg.getData().getTaskId();
		}
		return taskId;
	}
	
	/**
	 * 	批量推送
	 * @param taskId	任务ID
	 * @param cids		用户设备ID
	 */
	public static void pushListByCid(String taskId,List<String> cids) {
		GtApiConfiguration apiConfiguration = new GtApiConfiguration();  
		//填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取  
		apiConfiguration.setAppId("atuhrHeRDt5tFejagxm1R9");  
		apiConfiguration.setAppKey("atuhrHeRDt5tFejagxm1R9");  
		apiConfiguration.setMasterSecret("yktcHgzkJq9UdtXvBsqdq6");  
		apiConfiguration.setDomain("https://restapi.getui.com/v2/");  
		// 实例化ApiHelper对象，用于创建接口对象  
		ApiHelper apiHelper = ApiHelper.build(apiConfiguration);  
		// 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi  
		pushApi = apiHelper.creatApi(PushApi.class);  
         AudienceDTO audienceDTO = new AudienceDTO();
         audienceDTO.setTaskid(taskId);
         Audience audience = new Audience();
         for (String cid : cids) {
        	 audience.addCid(cid);
		}
         audienceDTO.setAudience(audience);
         System.out.println(pushApi.pushListByCid(audienceDTO));
	}
	public static void main(String[] args) {
		String title = "解锁微信啦";
		String content = "我来解锁你的微信啦 快同意呀";
		String groupName = "unlockWechat";
//		String taskId = "RASL_0519_406fae2b7e2648a093e66570cae811d0";
		// ApiResult{code=0, msg='success', data=TaskIdDTO{taskId='RASL_0519_16cdfb5c500f4f1280acf7b626d05804'}}
		String createMsg = createMsg(title, content, groupName);
		System.out.println(createMsg);
		List<String> cids = new ArrayList<String>();
		cids.add("5180e9c3d8da66d7983b98ae4d67e321");
//		cids.add("6972e54ba4c3a992cfdf87747a311b72");
//		cids.add("730f39b4fe070ee5f49fd6ae7e3cbc9a");
		pushListByCid(createMsg, cids);
	}

}
